# -*- coding: utf-8 -*-
# @Time : 2021/6/7 上午11:15
# @Author : fugang_le
import time

from src.lstm.predict import get_similay as lstm_similarity, get_similay_list
from src.similar_engine.similarity_algorithm import number_Jaccrad as jaccrad_similarity
from src.similar_engine.similarity_algorithm import sigmoid, LCS
from src.preprocess import special_process
from src.utils.util import time_difference
from src.utils.log_util import logger
from src.preprocess  import input_preprocess


def get_simility(text1, text2, es_score):
    lstm_score = lstm_similarity(text1, text2)
    jaccrad_score = jaccrad_similarity(text1,text2)
    # jaccrad_score = 0
    es_score = sigmoid(es_score)
    score = lstm_score * 0.2 + jaccrad_score * 0.2 + es_score * 0.6
    return score, lstm_score, jaccrad_score, es_score


def rerank(query_dict, candidate_dict):
    text1 = query_dict['materialName'] + query_dict['sepc']
    result = []
    start_time = time.time()
    for item in candidate_dict:
        text2 = special_process(item['_source']['materialName'], item["_source"]['classification']) + special_process(item['_source']['sepc'], item["_source"]['classification'])
        score, lstm_score, jaccrad_scor, es_score = get_simility(text1, text2, item['_score'])
        item['_source']['score'] = score
        item['lstm_score'] = lstm_score
        result.append(item)
    logger.info("get_simility spend time: {}ms".format(time_difference(start_time)))
    return sorted(result, key=lambda x:x['_source']['score'], reverse=True)


def rerank_batch(query_dict, candidate_dict):
    text1 = query_dict['materialName'] + query_dict['sepc']
    start_time = time.time()
    
    texts1 = []
    texts2 = []
    for item in candidate_dict:
        text2 = special_process(item['_source']['materialName'], item["_source"]['classification']) + special_process(item['_source']['sepc'], item["_source"]['classification'])
        text2 = input_preprocess(text2, '')
        texts2.append(text2)
        texts1.append(text1)
    start_time1 = time.time()
    lstm_scores = get_similay_list(texts1=texts1, texts2=texts2)
    logger.info("lstm model spend time: {}ms".format(time_difference(start_time1)))
    
    results = []
    for item, score, text2 in zip(candidate_dict, lstm_scores, texts2):
        lstm_score = score[0]
        es_score = item['_score']
        # print(es_score)
        jaccrad_score = jaccrad_similarity(text1,text2)
        lcs_score = LCS(text1, text2)
        es_score = sigmoid(es_score)
        score = lstm_score * 0.2 + jaccrad_score * 0.2 + es_score * 0.4 + lcs_score * 0.2
        # print('score:',score,"lstm score:",lstm_score, 'jaccrad_score',jaccrad_score, 'es_score',es_score, "lcs_score",lcs_score)
        # score = max(score, lstm_score)
        score = min(score, 1.0)
        item['_source']['score'] = score
        item['lstm_score'] = lstm_score
        results.append(item)
    logger.info("get_simility spend time: {}ms".format(time_difference(start_time)))
    return sorted(results, key=lambda x:x['_source']['score'], reverse=True)

        